package controllers

import "LoginTemplate/models"

type PayVisitController struct {
	BaseController
}

type PayVisit struct {
	ID         int    `json:"id"`
	Time       string `json:"time"`
	ClientName string `json:"client_name"`
	Phone      string `json:"phone"`
	Address    string `json:"address"`
	Content    string `json:"content"`
	Market     string `json:"market"`
	Post       string `json:"post"`
	Remark     string `json:"remark"`
	District   string `json:"district"`
}

func (u *PayVisitController) InsertPayVisit() {
	var payVisit PayVisit
	err := u.ShouldBind(&payVisit)
	if err != nil {
		println(err.Error())
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	sql := "insert into pay_visit (time,client_name,phone,address,content,market,post,remark,district) values (?,?,?,?,?,?,?,?,?)"
	tx := models.DB.Debug().Exec(sql, payVisit.Time, payVisit.ClientName, payVisit.Phone, payVisit.Address, payVisit.Content, payVisit.Market, payVisit.Post, payVisit.Remark, payVisit.District)
	if tx.Error != nil {
		u.JsonMsg(500, "", "失败")
	}
	u.JsonMsg(200, "", "成功")
}

type PayVisitData struct {
	Code        int        `json:"code"`
	Msg         string     `json:"msg"`
	Data        []PayVisit `json:"data"`
	Sum         int        `json:"sum"`         //总页数
	CurrentPage int        `json:"currentPage"` //当前页
	TotalNumber int        `json:"totalNumber"` //总条数
}
type PayVisitList struct {
	Page  int `json:"page"`
	Size  int `json:"size"`
	Id    int `json:"id"`
	Audit int `json:"audit"`
}

// 查询分类
func (u *PayVisitController) SelectPayVisit() {
	var pictureList PayVisitList
	err := u.ShouldBind(&pictureList)
	var sum int
	var totalNumber int
	if err != nil {
		println(err.Error())
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	var client []PayVisit
	sql := "select * from pay_visit  ORDER BY `district` limit ? , ?"
	tx := models.DB.Debug().Raw(sql, (pictureList.Page-1)*pictureList.Size, pictureList.Size).Scan(&client)
	if tx.Error != nil {
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	sqlsum := "SELECT CEIL(COUNT(*) / ? ) FROM pay_visit ORDER BY `district`"
	models.DB.Debug().Raw(sqlsum, pictureList.Size).Scan(&sum)
	sqlNumBer := "select count(*) from pay_visit ORDER BY `district`"
	models.DB.Debug().Raw(sqlNumBer).Scan(&totalNumber)
	var picturedata PayVisitData
	picturedata.Code = 200
	picturedata.Data = client
	picturedata.Msg = "成功"
	picturedata.Sum = sum
	picturedata.CurrentPage = pictureList.Page
	picturedata.TotalNumber = totalNumber
	u.JsonMsg(200, map[string]interface{}{
		"list":  picturedata,
		"count": "",
	}, "success")
}

func (u *PayVisitController) Select() {
	u.JsonMsg(200, map[string]interface{}{
		"list":  123,
		"count": "",
	}, "success")
}
